package com.example.mybatis_mytest.controller;

import com.example.mybatis_mytest.bean.Student;
import com.example.mybatis_mytest.bean.vo.StudentVo;
import com.example.mybatis_mytest.config.Result;
import com.example.mybatis_mytest.service.StudentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Api("contrlooer注解")
@Controller()
@ResponseBody
@RequestMapping("/student")
public class StudentController {
    //多个参数，使用java对象作为接口中方法的参数
    @Autowired
    private StudentService service;

    @RequestMapping(value = "/index", method = RequestMethod.GET)
    @ApiOperation(value = "查询所有的学生")
    public Result getStudentInfo() {
        List<StudentVo> studentInfo = service.getStudentInfo();
        return Result.ok(studentInfo);
    }

    /**
     * 一个简单类型的参数：
     * 简单类型： mybatis把java的基本数据类型和String都叫简单类型。
     * 在mapper文件获取简单类型的一个参数的值，使用 #{任意字符}
     */
    @RequestMapping(value = "/selectbyid", method = RequestMethod.GET)
    @ApiOperation(value = "根据学生的id进行查询")
    public Result getStudentByid(@RequestParam("id") Integer id) {
        Student student = service.selectStudentById(id);
        return Result.ok(student);
    }

    /**
     * 多个参数： 命名参数，在形参定义的前面加入 @Param("自定义参数名称")
     */
    @RequestMapping(value = "/SelectstudentBynameage", method = RequestMethod.GET)
    @ApiOperation(value = "根据学生的姓名和年龄查询")
    public Result getStudentBynameage(String name, Integer age) {
        List<StudentVo> studentVos = service.selectMultiParam(name, age);
        return Result.ok(studentVos);
    }

    /**
     * 多个参数，使用java对象作为接口中方法的参数
     */
    @RequestMapping(value = "/selectMultiStudent", method = RequestMethod.POST)
    @ApiOperation(value = "根据学生的姓名和密码查询")
    public Result selectMultiStudent(@RequestBody() Student s) {
        String password = s.getPassword();
        String username = s.getUsername();
        Student student=service.selectMultiStudent(username,password);
        return Result.ok(student);
    }

    @RequestMapping(value = "/selectMultiStudent2", method = RequestMethod.POST)
    @ApiOperation(value = "根据学生的实体进行查询")
    public Result selectMultiStudent2(@RequestBody() Student s) {
        Student student=service.selectMultiStudent2(s);
        return Result.ok(student);
    }
    /**
     * 多个参数，使用Map存放多个值
     */
    @ApiOperation(value = "根据学生的实体进行查询(map)")
    @RequestMapping(value = "/selectByMap",method = RequestMethod.POST)
    public Result selectByMap(@RequestBody Map<String,Object> map,HttpServletRequest request){
        System.out.println("map = " + map);
        StudentVo students = service.selectMultiByMap(map);
        return Result.ok(students);
    }
}
